
#include "gsl_ext_multimin.h"

gsl_ext_multimin_fd2fminimizer *
gsl_ext_multimin_fd2fminimizer_alloc (const gsl_ext_multimin_fd2fminimizer_type * T,
                                    size_t n)
{
  int status;

  gsl_ext_multimin_fd2fminimizer *s =
    (gsl_ext_multimin_fd2fminimizer *) malloc (sizeof (gsl_ext_multimin_fd2fminimizer));

  if (s == 0)
    {
      GSL_ERROR_VAL ("failed to allocate space for minimizer struct",
                     GSL_ENOMEM, 0);
    }

  s->type = T;

  s->x = gsl_vector_calloc (n);

  if (s->x == 0) 
    {
      free (s);
      GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
    }

  s->gradient = gsl_vector_calloc (n);

  if (s->gradient == 0) 
    {
      gsl_vector_free (s->x);
      free (s);
      GSL_ERROR_VAL ("failed to allocate space for gradient", GSL_ENOMEM, 0);
    }

  s->hessian = gsl_matrix_calloc(n, n);
  
  if (s->hessian == 0)
    {
      gsl_vector_free (s->x);
      gsl_vector_free (s->gradient);
      free (s);
      GSL_ERROR_VAL ("failed to allocate space for hessian", GSL_ENOMEM, 0);
    }
  
  s->dx = gsl_vector_calloc (n);

  if (s->dx == 0) 
    {
      gsl_vector_free (s->x);
      gsl_vector_free (s->gradient);
      gsl_matrix_free (s->hessian);
      free (s);
      GSL_ERROR_VAL ("failed to allocate space for dx", GSL_ENOMEM, 0);
    }
  
  s->state = malloc (T->size);
  
  if (s->state == 0)
    {
      gsl_vector_free (s->x);
      gsl_vector_free (s->gradient);
      gsl_matrix_free (s->hessian);
      gsl_vector_free (s->dx);
      free (s);
      GSL_ERROR_VAL ("failed to allocate space for minimizer state",
		     GSL_ENOMEM, 0);
    }

  status = (T->alloc) (s->state, n);

  if (status != GSL_SUCCESS)
    {
      free (s->state);
      gsl_vector_free (s->x);
      gsl_vector_free (s->gradient);
      gsl_matrix_free (s->hessian);
      gsl_vector_free (s->dx);
      free (s);

      GSL_ERROR_VAL ("failed to initialize minimizer state", GSL_ENOMEM, 0);
    }

  return s;
}

int
gsl_ext_multimin_fd2fminimizer_set (gsl_ext_multimin_fd2fminimizer * s,
                                  gsl_ext_multimin_function_fd2f * fd2f,
                                  const gsl_vector * x,
                                  double step_size, double tol)
{
  if (s->x->size != fd2f->n)
    {
      GSL_ERROR ("function incompatible with solver size", GSL_EBADLEN);
    }
  
  if (x->size != fd2f->n) 
    {
      GSL_ERROR ("vector length not compatible with function", GSL_EBADLEN);
    }  
    
  s->fd2f = fd2f;

  gsl_vector_memcpy (s->x, x);
  gsl_matrix_set_identity(s->hessian);
  gsl_vector_set_zero (s->dx);
  
  return (s->type->set) (s->state, s->fd2f, s->x, &(s->f), s->gradient, s->hessian, step_size, tol);
}

void
gsl_ext_multimin_fd2fminimizer_free (gsl_ext_multimin_fd2fminimizer * s)
{
  (s->type->free) (s->state);
  free (s->state);
  gsl_matrix_free (s->hessian);
  gsl_vector_free (s->dx);
  gsl_vector_free (s->gradient);
  gsl_vector_free (s->x);
  free (s);
}

int
gsl_ext_multimin_fd2fminimizer_iterate (gsl_ext_multimin_fd2fminimizer * s)
{
  return (s->type->iterate) (s->state, s->fd2f, s->x, &(s->f), s->gradient, s->hessian, s->dx);
}

int
gsl_ext_multimin_fd2fminimizer_restart (gsl_ext_multimin_fd2fminimizer * s)
{
  return (s->type->restart) (s->state);
}

const char * 
gsl_ext_multimin_fd2fminimizer_name (const gsl_ext_multimin_fd2fminimizer * s)
{
  return s->type->name;
}

gsl_vector * 
gsl_ext_multimin_fd2fminimizer_x (const gsl_ext_multimin_fd2fminimizer * s)
{
  return s->x;
}

gsl_vector * 
gsl_ext_multimin_fd2fminimizer_dx (const gsl_ext_multimin_fd2fminimizer * s)
{
  return s->dx;
}

gsl_vector * 
gsl_ext_multimin_fd2fminimizer_gradient (const gsl_ext_multimin_fd2fminimizer * s)
{
  return s->gradient;
}

gsl_matrix * 
gsl_ext_multimin_fd2fminimizer_hessian (const gsl_ext_multimin_fd2fminimizer * s)
{
  return s->hessian;
}

double 
gsl_ext_multimin_fd2fminimizer_minimum (const gsl_ext_multimin_fd2fminimizer * s)
{
  return s->f;
}
